package com.atguigu.gulimall.product.web;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class RedisLockController {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //可重入锁
    @GetMapping("/hello")
    public String hello(){
        RLock lock = redissonClient.getLock("my-lock");
        lock.lock(10, TimeUnit.SECONDS);
        try{
            System.out.println("加锁成功,执行业务..."+Thread.currentThread().getId());
        }catch (Exception e){

        }finally {
            System.out.println("解锁成功,执行业务..."+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }

    // 公平锁
    public void getFairLock(){
        RLock fairLock = redissonClient.getFairLock("anyLock");
        fairLock.lock();
    }

    /**
     * 缓存-分布式锁-Redission-读写锁补充
     * 读 + 读 ： 相当于无锁，并发读。只会在redis中记录好，所有当前的读锁。他们都会同时加锁成功
     * 写 + 读 ： 等待写锁释放
     * 写 + 写  ：阻塞等待
     * 读 + 写 ：等待读锁释放才能进行写操作
     */

    // 读写锁-写方法
    @GetMapping("/write")
    public String getReadWriteLock(){
        String s = "";
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock writeLock = lock.writeLock();
        try{
            // 加写锁
            writeLock.lock();
            System.out.println("写锁加锁成功...."+Thread.currentThread().getId());
            Thread.sleep(30000);
            s = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set("writeValue",s);
        }catch (Exception e){

        }finally {
            // 释放写锁
            writeLock.unlock();
            System.out.println("写锁释放成功..."+Thread.currentThread().getId());
        }
        return s;
    }

    // 读写锁-读方法
    @GetMapping("/read")
    public String readValue(){
        String s = "";
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = lock.readLock();
        try{
            rLock.lock();
            s = redisTemplate.opsForValue().get("writeValue");
        }catch (Exception e){

        }finally {
            // 释放写锁
            rLock.unlock();
        }
        return s;
    }

    // 闭锁模式 模拟场景:放假学校锁门,只有当5个班的人都走光了,门卫才能锁门
    @GetMapping("/lock/door")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        System.out.println("方法走到这里了....");
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5); // 设置锁的计数器是5，当变成1时就继续往下执行
        door.await(); //等待闭锁都完成
        return "放假了,锁门了...";
    }

    @GetMapping("/gogogo/{id}")
    @ResponseBody
    public String gogogo(@PathVariable("id") Long id){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown(); //计数器减1
        return id + "班人都走了...";
    }

    // 信号量测试
    @ResponseBody
    @GetMapping("/park")
    public String park() throws InterruptedException {
        RSemaphore semaphore = redissonClient.getSemaphore("park");
        semaphore.acquire(); //阻塞方法,获取一个信息量,获取一个停车位
        return "停车成功";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go(){
        RSemaphore semaphore = redissonClient.getSemaphore("park");
        semaphore.release(); // 释放一个信号量,释放一个车位
        return "开走一辆车";
    }

}
